<?php
use common\components\helpers\Fun;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\components\extenders\MenuManage;

?>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<div>
    <div>
        <legend id="customer_info" data-student="<?= $student_count ?>" data-total="<?= $order_info ?>"
                data-deposit="<?= $deposit_param['deposit'] ?>"
                data-url="<?= Fun::url(['customer/consume', 'id' => $customer->id]) ?>"><h4>会员信息</h4></legend>
        <p style="color: #2f4f4f">会员名称:<?= $customer->name ?>&nbsp;当前可用余额:<?= $customer->account ?>元</p>
    </div>
    <div class="clear"></div>
    <div>
        <h4>预约课程</h4>

        <div style="border:1px solid #eee;padding:12px;width: 60%;margin-bottom: 10px">
            <p style="font-style: italic;color: #005fb3">*下表“数量”可编辑*</p>
            <table id="data-grid" class="easyui-datagrid" style="width: 100%" data-options="fitColumns:true,
            checkbox:true,
            rownumbers: true,
            singleSelect:false,
            idField: 'id',
            pagination:true,
            toolbar:'#toolbar'
            ">
                <thead>
                <tr>
                    <th data-options="field:'ck',checkbox:true,align:'center'"></th>
                    <th data-options="field:'number',name:'numberbox',align:'right',editor:{type:'numberbox',options:{min:1}}">
                        数量
                    </th>
                    <th data-options="field:'category_name',align:'center'"><?= \Yii::t('zhtfrontend', '类别名称'); ?></th>
                    <th data-options="field:'mechanic_name',align:'center'"><?= \Yii::t('zhtfrontend', '所属机构'); ?></th>
                    <th data-options="field:'total_stock',align:'center'"><?= \Yii::t('zhtfrontend', '库存总量'); ?></th>
                    <th data-options="field:'course_name',align:'center'"><?= \Yii::t('zhtfrontend', '课程名称'); ?></th>
                    <th data-options="field:'teacher_name',align:'center'"><?= \Yii::t('zhtfrontend', '教师名称'); ?></th>
                    <th data-options="field:'room_number',align:'center'"><?= \Yii::t('zhtfrontend', '教室编号'); ?></th>
                    <th data-options="field:'class_day',align:'center'"><?= \Yii::t('zhtfrontend', '上课星期'); ?></th>
                    <th data-options="field:'start_time',align:'center'"><?= \Yii::t('zhtfrontend', '上课时间'); ?></th>
                    <th data-options="field:'end_time',align:'center'"><?= \Yii::t('zhtfrontend', '下课时间'); ?></th>
                    <th data-options="field:'class_count',align:'center'"><?= \Yii::t('zhtfrontend', '上课次数'); ?></th>
                    <th data-options="field:'max_number',align:'center'"><?= \Yii::t('zhtfrontend', '满额人数'); ?></th>
                    <th data-options="field:'opening_number',align:'center'"><?= \Yii::t('zhtfrontend', '开班人数'); ?></th>
                    <th data-options="field:'price',align:'center'"><?= \Yii::t('zhtfrontend', '销售价'); ?></th>
                </thead>
            </table>
        </div>
        <div style="margin: 10px"> <span>
                 <a><?= (new MenuManage(MenuManage::TOP_MENU, ['name' => '添加课程']))->detailsMenu('customer', 'addcourse', false, ['rel' => 'dialog-check-box', 'class' => 'button', 'id' => 'appointment']) ?></a>
                &nbsp;
                <a><font id="remove" class="button">取消</font></a>&nbsp;
            </span>
        </div>
    </div>
    <?php $form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false, 'options' => ['class' => 'form-validate']]); ?>
    <table width="100%" class="table-form">
        <tr>
            <th width="80" align="right">定金金额</th>
            <td><?= $form->field($model, 'total', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:220px', 'readonly' => 'true', 'class' => 'input-text']) ?></td>
        </tr>
        <tr style="display: none">
            <th width="80" align="right"><?= $model->attributeLabels()['product']; ?></th>
            <td><?= $form->field($model, 'product', ['template' => '{input}{error}'])->hiddenInput(); ?></td>
        </tr>
        <tr style="display: none">
            <th width="80" align="right"><?= $model->attributeLabels()['number']; ?></th>
            <td><?= $form->field($model, 'number', ['template' => '{input}{error}'])->hiddenInput(); ?></td>
        </tr>
        <tr style="display: none">
            <th width="80" align="right"><?= $model->attributeLabels()['coupon_id']; ?></th>
            <td style="display: inline-block;width: 220px"><?= $form->field($model, 'coupon_id', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>$items,'options' => ['placeholder' => '请选择']]) ?></td>
        </tr>
        <tr style="display: none">
            <th width="80" align="right"><?= $model->attributeLabels()['discount_pay']; ?></th>
            <td><?= $form->field($model, 'discount_pay', ['template' => '{input}{error}'])->textInput(['style' => 'width:220px', 'value' => 0, 'readonly' => 'true', 'class' => 'input-text']) ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['is_appointment']; ?></th>
            <td style="display: inline-block;width: 220px"><?= $form->field($model, 'is_appointment', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>['1' => '预约科目订单', '2' => '预约类别订单'],'options' => ['placeholder' => '请选择']]) ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['actual_pay']; ?></th>
            <td><?= $form->field($model, 'actual_pay', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:220px', 'readonly' => 'true', 'value' => 0, 'class' => 'input-text']) ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['pay_card']; ?></th>
            <td><?= $form->field($model, 'pay_card', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:220px', 'value' => 0, 'class' => 'input-text']) ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['password']; ?></th>
            <td><?= $form->field($model, 'password', ['template' => '{input}{error}{hint}'])->passwordInput(['style' => 'width:220px', 'class' => 'input-text']); ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['bind_id']; ?></th>
            <td style="display: inline-block;width: 220px"><?= $form->field($model, 'bind_id', ['template' => '{input}{error}{hint}'])->widget(\kartik\select2\Select2::className(),['data'=>$student,'options' => ['placeholder' => '请选择','data-v_code' => $student_code, ]]) ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['v_code']; ?></th>
            <td><?= $form->field($model, 'v_code', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:220px', 'class' => 'input-text']); ?></td>
        </tr>
        <tr>
            <th width="80" align="right"><?= $model->attributeLabels()['remark']; ?></th>
            <td><?= $form->field($model, 'remark', ['template' => '{input}{error}{hint}'])->textarea(['rows' => 2, 'style' => 'width:220px', 'class' => 'input-textarea']) ?></td>
        </tr>
    </table>
    <div class="m_top_10 btn">
        <?= Html::submitButton('提交', ['class' => "button", 'id' => 'dosubmit', 'data' => ['number' => 0]]) ?>&nbsp;
        <a href=<?= $url['back'] ?>><input type='button' class="button" value="返回"/></a>
    </div>
    <?php ActiveForm::end(); ?>
</div>
<?php
$js = <<<JS
    var data_view = $.extend({}, $.fn.datagrid.defaults.view, {
        onAfterRender: function (target) {
            $(target).prev().find('.datagrid-header-inner').width($(target).prev().find('.datagrid-header').width());
            $(target).prev().find('.datagrid-htable').width('100%');
            $(target).prev().find('.datagrid-htable div').each(function (i, item) {
                if ($(item).css('text-align') == 'center') {
                    $(item).css('margin', '0 auto');
                }
            });
            $(target).prev().find('.datagrid-btable div').each(function (i, item) {
                if ($(item).css('text-align') == 'center') {
                    $(item).css('margin', '0 auto');
                }
            });
        }
    });
    //验证码
    $('#order-bind_id').change(function(){
       var student_id=$(this).val();
       var v_code=JSON.parse($(this).attr("data-v_code"));
       if(v_code!=null){
            $('#order-v_code').val(v_code[student_id]);
        }
    });
    //预约
    $(document).on('click', 'a[id=appointment]', function () {
        var student_count=Number($('#customer_info').data('student'));
        var total_number=Number($('#customer_info').data('total'));
        var max_number=3*student_count;
        if(student_count==0){alert('请先绑定学员信息！');return false;}
        else if(total_number>=max_number){alert('该用户绑定了'+student_count+'名学员,已经预约'+max_number+'门！');return false;}
        else{
        var dialog_size = $(this).data('size');
        var dialog_witdh = dialog_size.indexOf('*') > 0 ? dialog_size.split('*')[0] : 500;
        var dialog_height = dialog_size.indexOf('*') > 0 ? dialog_size.split('*')[1] : 500;
        window.top.$('#dialog-create', top.document).dialog({
            title: $(this).html(),
            width: dialog_witdh,
            height: dialog_height,
            resizable: true,
            closed: false,
            cache: false,
            content: '<iframe id="form-iframe" marginheight="0" marginwidth="0" frameborder="0" scrolling="yes" style="width:100%; height:100%;" src="' + $(this).data('url') + '">',
            modal: true,
            buttons: [{
                text: '添加',
                id: 'add',
                handler: function () {
                    var array = window.top.document.getElementById('form-iframe').contentWindow.checkbox_selected_value;
                    var row = $('#data-grid').datagrid('getRows');
                    if (row.length == 0) {
                        if(array.length>1){alert("最多预约1门！");return false;}
                        else{
                            for (var i = 0; i < array.length; i++) {
                                array[i]['number'] = 1;
                                $('#data-grid').datagrid('appendRow', array[i]);
                            }
                        }
                    }
                    else {
                        var length=getOrdernum();
                        var flag = true;
                        for (var j = 0; j < array.length; j++)
                        {
                            for (var r = 0; r < row.length; r++) {
                                if (array[j].id == row[r].id) {
                                    flag = false;
                                    break;
                                }
                                else {
                                    flag = true;
                                }

                            }
                            if (flag == true) {
                                array[j]['number'] = 1;
                                length +=array[j]['number'];
                                if (length >1) {alert("最多预约1门！");clearDialog();}
                                else{
                                    $('#data-grid').datagrid('appendRow', array[j]);
                                }
                            }
                        }
                    }
                    var deposit=$('#customer_info').data('deposit');
                    $("#order-total").val(accMul(getOrdernum(),deposit));
                    $("#order-actual_pay").val(accMul(getOrdernum(),deposit));
                    $("#order-product").val(getIds());
                    $("#order-number").val(getNumber());
                    $('#data-grid').datagrid({
                        data: $('#data-grid').datagrid('getRows'),
                        view: data_view,
                        onResize: function (width, height) {
                            $('.datagrid-header-inner', $(this)).each(function (i, item) {
                                $(item).width($(item).parent().width());
                            });
                        },
                        onCheck: function (rowIndex, rowData) {
                            checkbox_selected_value = $(this).datagrid('getSelections');
                        }
                    }).datagrid('enableCellEditing');
                    clearDialog();
                }

            }, {
                text: '关闭',
                handler: function () {
                    clearDialog();
                }
            }]
        });
    }
    });
    //移除商品
    $('#remove').click(function () {
        $(".datagrid-view2 tbody").find('.datagrid-row-checked').each(function () {
            $('#data-grid').datagrid('deleteRow', $(this).index());
        });
        var deposit=$('#customer_info').data('deposit');
        $("#order-total").val(accMul(getOrdernum(),deposit));
        $("#order-actual_pay").val(accMul(getOrdernum(),deposit));
        $("#order-product").val(getIds());
        $("#order-number").val(getNumber());
    });
    //返回选中商品id
    function getIds() {
        var data = $('#data-grid').datagrid('getRows');
        var ids = '';
        for (var i = 0; i < data.length; i++) {
            (i == 0) ? ids = data[i]['id'] : ids += ',' + data[i]['id'];
        }
        return ids;
    }
    //返回选中商品数量
    function getNumber() {
        var data = $('#data-grid').datagrid('getRows');
        var number = '';
        for (var i = 0; i < data.length; i++) {
            (i == 0) ? number = data[i]['number'] : number += ',' + data[i]['number'];
        }
        return number;
    }
    //
    function getOrdernum() {
        var row = $('#data-grid').datagrid('getRows');
        var subject_num=0;
        if(row.length==0){return 0;}
        else{
            for (var i = 0; i < row.length; i++) {
                subject_num+=row[i]['number'];
            }
            return parseInt(subject_num);
        }
    }
    //
    $.extend($.fn.datagrid.methods, {
        editCell: function (jq, param) {
            return jq.each(function () {
                var opts = $(this).datagrid('options');//得到该条记录的对象
                var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));//获取冻结列与解冻列名称组成的数组["ck", "number", "category_name", "mechanic_name", "total_stock", "course_name", "teacher_name", "room_number", "class_day", "class_time", "class_count", "max_number", "opening_number", "price", "deposit"]
                for (var i = 0; i < fields.length; i++) {
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor1 = col.editor;
                    if (fields[i] != param.field) {
                        col.editor = null;
                    }
                }
                $(this).datagrid('beginEdit', param.index);
                var ed = $(this).datagrid('getEditor', param);
                if (ed) {
                    if ($(ed.target).hasClass('textbox-f')) {
                        $(ed.target).textbox('textbox').focus();
                    } else {
                        $(ed.target).focus();
                    }
                }
                for (var i = 0; i < fields.length; i++) {
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor = col.editor1;
                }
            });
        },
        enableCellEditing: function (jq) {
            return jq.each(function () {
                var dg = $(this);
                var opts = dg.datagrid('options');
                opts.oldOnClickCell = opts.onClickCell;
                opts.onClickCell = function (index, field) {
                    if (opts.editIndex != undefined) {
                        if (dg.datagrid('validateRow', opts.editIndex)) {
                            dg.datagrid('endEdit', opts.editIndex);
//                            var total_number='<?//=$order_info?>//';
                            var data = $('#data-grid').datagrid('getRows');
//                            var subject_num =0;
                            for (var m = 0; m < data.length; m++) {
                                if (data[m].number > data[m].total_stock) {
                                    alert("库存数量不足！");
                                    $('#data-grid').datagrid('updateRow',{index: m, row: {number: 1}});
                                    return false;
                                }
                                else if (data[m].number >1) {
                                    alert("最多预约1门课程！");
                                    $('#data-grid').datagrid('updateRow',{index: m, row: {number: 1}});
                                    return false;
                                }
                                }
                            opts.editIndex = undefined;
                            var deposit=$('#customer_info').data('deposit');
                            $("#order-total").val(accMul(getOrdernum(),deposit));
                            $("#order-actual_pay").val(accMul(getOrdernum(),deposit));
                            $("#order-product").val(getIds());
                            $("#order-number").val(getNumber());
                        } else {
                            return;
                        }
                    }
                    dg.datagrid('selectRow', index).datagrid('editCell', {
                        index: index,
                        field: field
                    });
                    opts.editIndex = index;
                    opts.oldOnClickCell.call(this, index, field);
                }
            });
        }
    });
    //
    function reloadData(){
        document.location.reload();
    }
JS;
$this->registerJS($js);
?>


